package photogcalc;

import photogcalc.common.Constants;
import photogcalc.i18n.I18n;
import photogcalc.util.Settings;
import photogcalc.i18n.I18nFactory;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.ChoiceGroup;

import javax.microedition.lcdui.Screen;
import net.sf.microlog.core.Logger;
import net.sf.microlog.core.LoggerFactory;

/**
 * 
 * @author Antti Okkonen
 */
public class PropertiesView implements CommandListener {
    // Main form and it's items
    private Screen mainForm;
    private Command saveCmd;
    private Command cancelCmd;
    private ChoiceGroup decimalOrAny;
	
    // Main display
    private PropertiesCommandListener listener;
    private Settings settings;
    private I18n i18n;

    private Logger logger = LoggerFactory.getLogger(getClass().getName());
    
    public PropertiesView(PropertiesCommandListener listener, Settings settings) {
	this.listener = listener;
        this.settings = settings;
        this.i18n = I18nFactory.createI18n(System.getProperty("microedition.locale"));

        // Create UI
        mainForm = createParamsUI();
//        cc.display(mainForm);
    }

    public void commandAction(Command c, Displayable d) {
        // Save input data to the datamodel
        saveSettings();

        if (c == cancelCmd) {
            listener.cancelCommand();
        } 
        else if (c == saveCmd) {
            listener.saveCommand();
        }
        else {
            logger.warn("Unknown command " + c.getLabel());
        }
    }

    protected Form createParamsUI() {
        // TODO: make slider / textfield optional
        // TODO: make numeric / text option disabled when there is not any
        // textfields on the screen
        Form form = new Form(i18n.get("_Asetukset")/* + " - " + Constants.TITLE*/);

        // Add check box
        decimalOrAny = new ChoiceGroup(i18n.get("_Tekstikentan oletus"), ChoiceGroup.EXCLUSIVE);
        decimalOrAny.append(i18n.get("_numerot"), null);
        decimalOrAny.append(i18n.get("_kirjaimet"), null);
        
        if (settings.get("textfielddefault") != null) {
            int index = Integer.parseInt(settings.get("textfielddefault"));
            decimalOrAny.setSelectedIndex(index, true);
        }
        else {
            decimalOrAny.setSelectedIndex(0, true);
        }

        form.append(decimalOrAny);

        // Add commands to menu
        cancelCmd = new Command(i18n.get("_Takaisin"), Command.CANCEL, 1);
        saveCmd = new Command(i18n.get("_Tallenna"), Command.OK, 2);
        form.addCommand(cancelCmd);
        form.addCommand(saveCmd);
        form.setCommandListener(this);

        return form;
    }
	
    protected void saveSettings() {
        String tmp = new Integer(decimalOrAny.getSelectedIndex()).toString();
        settings.put("textfielddefault", tmp);
    }

    public Screen getScreen() {
        return mainForm;
    }
}
